#include <stdio.h>
#include <malloc.h>
#include "btree.h"

void CreateBTNode(BTNode *&b,char *str)     //由str串创建二叉链
{
   BTNode *St[MaxSize],*p;
   int top = -1,k,j=0;
   char ch;
   b=NULL;
   ch=str[j];
   while(ch!='\0')
   {
       switch(ch){
   case '(':top++;St[top]=p;k=1;break;
   case ')':top--;break;
   case ',':k=2;break;
   default:p=(BTNode *)malloc(sizeof(BTNode));
           p->data=ch;
           p->lchild=p->rchild=NULL;
           if(b==NULL)
                b=p;
            else{
                switch(k){
                    case 1:St[top]->lchild=p;break;
                    case 2:St[top]->rchild=p;break;
                }
            }
   }
   j++;
   ch=str[j];
   }
}
BTNode *FindNode(BTNode *b,ElemType x)  //返回data域为x的节点指针
{
    BTNode *p;
    if(b==NULL)
        return NULL;
    else if(b->data==x)
        return b;
    else{
        p=FindNode(b->lchild,x);
        if (p!=NULL)
            return p;
        else  
            return FindNode(b->rchild,x);
    }
}
BTNode *LchildNode(BTNode *p)   //返回*p节点的左孩子节点指针
{
    return p->lchild;
}
BTNode *RchildNode(BTNode *p)   //返回*p节点的右孩子节点指针
{
    return p->rchild;
}
int BTNodeDepth(BTNode *b)  //求二叉树b的深度
{
   int lchildh,rchildh;
   if (b==NULL) return(0);
   else{
       lchildh=BTNodeDepth(b->lchild);
       rchildh=BTNodeDepth(b->rchild);
       return (lchildh>rchildh) ? (lchildh+1):(rchildh+1);
   }
   
}
void DispBTNode(BTNode *b)  //以括号表示法输出二叉树
{
   if(b!=NULL){
       printf("%c",b->data);
       if(b->lchild!=NULL || b->rchild!=NULL){
           printf("(");
           DispBTNode(b->lchild);
           if(b->rchild!=NULL) printf(",");
           DispBTNode(b->rchild);
           printf(")");
       }
   }

}
void DestroyBTNode(BTNode *&b)   //销毁二叉树
{
    if(b!=NULL){
        DestroyBTNode(b->lchild);
        DestroyBTNode(b->rchild);
        free(b);
    }
}